<?php

namespace App\Http\Controllers\Api;

use App\Http\Queries\GoodsCollectionQuery;
use App\Http\Requests\Api\GoodsCollectionRequest;
use App\Http\Resources\GoodsCollectionResource;
use Illuminate\Http\Request;
use App\Models\GoodsCollection;
class GoodsCollectionController extends Controller
{
    //用户的收藏
    public function index(Request $request,GoodsCollectionQuery $query){
        $page_size = $request->page_size?$request->page_size:10;
        $collections = $query->where("user_id",$request->user()->id)->paginate($page_size);
        return GoodsCollectionResource::collection($collections);
    }
    //
    //收藏产品
    public function store(GoodsCollectionRequest $request, GoodsCollection $goodsCollection){
        $goodsCollection->user()->associate($request->user());
        $goodsCollection->goods_id = $request->goods_id;
        $goodsCollection->save();
        return response()->json(['msg'=>"收藏成功"])->setStatusCode(201);
    }

    //取消收藏产品
    public function destroy(GoodsCollectionRequest $request){
        GoodsCollection::
        where(['user_id'=>$request->user()->id,"goods_id"=>$request->goods_id])
            ->delete();
        return response()->json(['msg'=>"取消收藏成功"])->setStatusCode(201);
    }


}
